<?php

# 下单和重新下单 请添加callbackUrl 订单状态回调函数
class ddController extends AppController
{
    private $order_id = '2019091190';

    /**
     * 获取城市code
     */
    public function getCityCodeAction()
    {
        $dde = new DDExpressClass(TRUE);
        $result =$dde->getCityCode();
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 下单
     * shop_no  门店编号
     * origin_id    第三方订单id
     * city_code    城市代码
     * cargo_price  订单金额
     * is_prepay    是否需要垫付 1:是 0:否 (垫付订单金额，非运费)
     * receiver_name    收货人姓名
     * receiver_address 收货人地址
     * receiver_lat 收货人地址维度（高德坐标系）
     * receiver_lng 收货人地址经度（高德坐标系）
     * callback 回调URL
     * receiver_phone 收货人手机号
     */
    public function send_parameterAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');

        $parameter = [
            'shop_no' => '11047059',
            'origin_id' => $this->order_id,
            'city_code' => '021',
            'cargo_price' => 10,
            'is_prepay' => 0,
            'receiver_name' => 'Ethan',
            'receiver_address' => '上海市宝山区吴淞街道永清二村',
            'receiver_lat' => 31.3882634020,
            'receiver_lng' => 121.4947557449,
            'callback' => 'http://zhaipei.shjieta.com.cn/?md=index&cl=dd&at=DDgetcallback',
            'receiver_phone' => '17321295203',
        ];
        $result = $dde->send_parameter1($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 重新下单
     */
    public function re_send_parameterAction()
    {
        $dde = new DDExpressClass(TRUE,19529,'dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'shop_no' => '11047059',
            'origin_id' => $this->order_id,
            'city_code' => '021',
            'cargo_price' => 10,
            'is_prepay' => 0,
            'receiver_name' => 'Ethan',
            'receiver_address' => '上海市宝山区吴淞街道永清二村',
            'receiver_lat' => 31.3882634020,
            'receiver_lng' => 121.4947557449,
            'callback' => 'http://zhaipei.shjieta.com.cn/?md=index&cl=dd&at=DDgetcallback',
            'receiver_phone' => '17321295203',
        ];
        $result = $dde->re_send_parameter($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 查询订单
     */
    public function queryAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
//            'order_id' => $this->order_id,
            'order_id' => '100046_PH19030200000821_5',
        ];
        $result = $dde->query($parameter);
        echo '<pre>';
        var_dump($result);
        return $result;
    }



    /**
     * 查询订单运费后下单
     * 1.预发布订单的操作流程是：使用【查询订单运费接口】获取平台订单编号，调用【查询运费后发单接口】即可发布订单。
     * 2.【查询订单运费接口】经纬度为非必传参数，如果不传经纬度，系统会自动在city_code所在城市解析收货地址，所以此时接口中的city_code一定要传正确。
     */
    public function queryDeliverFeeAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');

        $parameter = [
            'shop_no' => '11047059',
            'origin_id' => $this->order_id,
            'city_code' => '021',
            'cargo_price' => 10,
            'is_prepay' => 0,
            'receiver_name' => 'Ethan',
            'receiver_address' => '上海市宝山区吴淞街道永清二村',
            'receiver_lat' => 31.3882634020,
            'receiver_lng' => 121.4947557449,
            'callback' => 'http://zhaipei.shjieta.com.cn/?md=index&cl=dd&at=DDgetcallback',
            'receiver_phone' => '17321295203',
        ];
        $result = $dde->queryDeliverFee($parameter);
        return $result['result']['deliveryNo'];        # 提供给查询运费后下单
    }


    /**
     * 查询运费后下单
     * 1.预发布订单的操作流程是：使用【查询订单运费接口】获取平台订单编号，调用【查询运费后发单接口】即可发布订单。
     * 2.【查询订单运费接口】经纬度为非必传参数，如果不传经纬度，系统会自动在city_code所在城市解析收货地址，所以此时接口中的city_code一定要传正确。
     */
    public function addAfterQueryAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'deliveryNo' => $this->queryDeliverFeeAction(),
        ];
        $result = $dde->addAfterQuery($parameter);
        echo '<pre>';
        var_dump($result);
    }



    /**
     * 添加小费
     */
    public function addTipAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'order_id' => $this->order_id,
            'tips' => 1.2,
            'city_code' => '021',
            'info' => '辛苦了...',
        ];
        $result = $dde->addTip($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 取消订单理由列表 取消订单时 cancel_reason_id = $res['result'][0]['id']
     * 目前有10大理由
     * $res['result'][0]['reason'] = [0]没有配送员接单&id=1，[1]=配送员没来取货&id=2 [2]=配送员态度太差&id=3 [3]=顾客取消订单&id=4 [4]=订单填写错误&id=5
     *                              [5]=配送员让我取消此单&id=34 [6]=配送员不愿上门取货&id=35 [7]=我不需要配送了&id=36 [8]=配送员以各种理由表示无法完成订单&id=37 [9]=其他&id=10000
     *
     */
    public function reasonsReasonsAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');

        $result = $dde->reasonsReasons();
        return $result['result'];
    }


    /**
     * 取消订单
     */
    public function formalCancelAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $cancel_reason = $this->reasonsReasonsAction();
        $cancel_reason_id = 1;
        $parameter = [
            'order_id' => '100614_PH19061200000006_99',
            'cancel_reason_id' => $cancel_reason_id,
            'cancel_reason'=>'取消原因(当取消原因ID为其他时，此字段必填)',
        ];
        if($cancel_reason_id == 10000){
            $parameter['cancel_reason'] = '取消原因(当取消原因ID为其他时，此字段必填)';
        }
        $result = $dde->formalCancel($parameter);
        echo '<pre>';
        var_dump($result);
    }



    /**
     * 查询追加配送员
     */
    public function transporterAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $result = $dde->transporter();
        if($result['status'] == 'success'){
            return $result['result'];
        }
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 追加订单
     *  追加的订单必须是该门店发出的处于待接单状态的订单
     * 订单状态(待接单＝1 待取货＝2 配送中＝3 已完成＝4 已取消＝5 已过期＝7 指派单=8 妥投异常之物品返回中=9 妥投异常之物品返回完成=10 系统故障订单发布失败=1000 可参考文末的状态说明）
     */
    public function existAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $OrderStatus = $this->queryAction();
        $transporter = $this->transporterAction();  # 返回二维数组
        if($OrderStatus['status'] == 'success' &&  $OrderStatus['result']['statusCode'] == 1 ){
            $parameter = [
                'order_id' => $this->order_id,
                'transporter_id' => $transporter[0]['id'],
            ];
            $result = $dde->exist($parameter);
            echo '<pre>';
            var_dump($result);
        }else{
         echo '配送员已接单 当前不可追加订单！';
         exit;
        }
    }


    /**
     * 取消追加订单
     */
    public function cancelExistAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'order_id' => $this->order_id,
        ];
        $result = $dde->cancelExist($parameter);
        echo '<pre>';
        var_dump($result);
    }



    /**
     * 投诉原因列表
     */
    public function complaintListAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $result = $dde->complaintList();
        if($result['status'] == 'success'){
            return $result['result'];
        }
        echo '<pre>';
        var_dump($result);
    }



    /**
     * 投诉
     */
    public function complaintAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $reason_id = $this->complaintListAction();
        $parameter = [
            'order_id' => $this->order_id,
            'reason_id' => $reason_id[0]['id'],
        ];
        $result = $dde->complaint($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 返投状态（状态9）后 收货确认 修改状态 为 确认收货 （状态10）
     */
    public function confirmErrorAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'order_id' => $this->order_id,
        ];
        $result = $dde->confirmError($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 注册商户
     */
    public function regMerchantAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
            'mobile' => '17321295203',
            'city_name' => '上海市',
            'enterprise_name' => 'Ethan',
            'enterprise_address' => '上海市虹口区凉城新村街道馨苑东',
            'contact_name' => 'Ethan',
            'contact_phone' => '17321295203',
            'email' => '534592533@qq.com',
        ];
        $result = $dde->regMerchant($parameter);
        echo '<pre>';
        var_dump($result);
    }

    /**
     * 注册商户 门店信息为二维数组 可传多个
     * business = 业务类型(食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29, 其他-5)
     * origin_shop_id = 门店编码,可自定义,但必须唯一;若不填写,则系统自动生成
     * username = 达达商家app账号(若不需要登陆app,则不用设置）
     * password = 达达商家app密码(若不需要登陆app,则不用设置)
     */
    public function addShopAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [0=>[
           'station_name' => '测试2',
           'business' => 1,
           'city_name' => '上海市',
           'area_name' => '浦东',
           'station_address' => '上海市浦东新区张江镇钱堂村',
           'lng' => 121.6003704071,
           'lat' => 31.1669112268,
           'contact_name' => 'Ethan',
           'phone' => '17321295203',
//           'origin_shop_id'=>'',
//           'id_card'=>'',
//           'username'=>'',
//           'password'=>'',
        ]];
        $result = $dde->addShop($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 修改门店信息
     * origin_shop_id =  在 body 必传   其他可都不传
     * status = 门店状态（1-门店激活，0-门店下线）
     * contact_name 联系人
     */
    public function updateShopAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $originShopId = '73753-36559';  # 门店编号
        $parameter = [
           'origin_shop_id' => $originShopId,
           'new_shop_id' => '',
            'station_name' => '测试修改门店信息',
            'business' => 1,
            'city_name' => '上海市',
            'area_name' => '浦东',
            'station_address' => '上海市浦东新区张江镇钱堂村',
            'lng' => 121.6003704071,
            'lat' => 31.1669112268,
            'contact_name' => 'Ethan',
            'phone' => '17321295203',
            'status' => 0,
        ];
        $result = $dde->updateShop($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 查看门店信息
     */
    public function detailAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $originShopId = '73753-36559';  # 门店编号
        $parameter = [
           'origin_shop_id' => $originShopId,
        ];
        $result = $dde->detail($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 查看门店信息
     * 查询运费账户类型（1：运费账户；2：红包账户，3：所有），默认查询运费账户余额
     * 返回   redPacketBalance = 红包    deliverBalance = 余额
     */
    public function balanceQueryAction()
    {
        $dde = new DDExpressClass(TRUE,'19529','dada49da12778ddac4b','2d5a37a0ba36548b084aa5820551073e');
        $parameter = [
           'category' => 3,
        ];
        $result = $dde->balanceQuery($parameter);
        echo '<pre>';
        var_dump($result);
    }


    /**
     * 订单状态(待接单＝1,待取货＝2,配送中＝3,已完成＝4,已取消＝5, 已过期＝7,指派单=8,妥投异常之物品返回中=9, 妥投异常之物品返回完成=10,骑士到店=100,创建达达运单失败=1000）
     */
    public function DDgetcallbackAction(){
        $body = file_get_contents('php://input');
        file_put_contents('logs/DDgetcallback',$body);
        $returnInfo = json_decode($body,true);
        $statusNum = [1,2,3,4,5,7,8,9,10,100,1000];
        if(in_array($returnInfo['order_status'],$statusNum)){
//            $return = ['status'=>'success','code'=>0,'msg'=>'成功'];
//            echo json_encode($return);
            echo json_encode(200);
        }
    }

}
